import Vue from 'vue'
import vuex from 'vuex'
import { getToken, removeToken, setToken, getUserInfo, setUserInfo, removeUserInfo } from '@/utils/auth'

Vue.use(vuex);

export default new vuex.Store({
  state: {
    token: getToken(),
    userInfo: getUserInfo()
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_DISHES: (state, dishes) => {
      state.dishes = dishes
    },
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo;
    }
  },
  actions: {
    // 前端 登出
    FedLogOut({ commit }) {
      commit('SET_TOKEN', '')
      commit('SET_USER_INFO', '')
      removeToken()
      removeUserInfo()
    },
    SetToken({ commit }, token) {
      commit('SET_TOKEN', token);
      setToken(token);
    },
    SetUserInfo({ commit }, userInfo) {
      commit('SET_USER_INFO', userInfo)
      setUserInfo(userInfo);
    }
    // SetUserInfo({ commit }, userInfo) {
    //   return new Promise(resolve => {
    //     commit('SET_USER_INFO', userInfo)
    //     resolve()
    //   });
    // }
    // SetDishes({ commit }, dishes) {
    //   return new Promise(resolve => {
    //     commit('SET_DISHES', dishes)
    //     setDishes(dishes)
    //     resolve()
    //   })
    // }
  }
})